<?php 

//include 'Model.class.php';

class frdModel extends Model
{
	private $frd='frd';

	//添加新的好友条目
	public function add($uid,$data)
	{
		$this->db->lock($this->frd);
		$data['ownid']=$uid;

		$sql='insert ignore into '.$this->frd.' (';
		$sql.=implode(',', array_keys($data)).') values ("';
		$sql.=implode('","', array_values($data)).'")';
		/*
		if($this->db->set_query($sql))
		{

			return $this->db->insert_id();
		}
		else
		{
			return false;
		}
		*/
		$add=$this->db->set_query($sql);
		$this->db->unlock();
		return $add;
	}

	//通过好友组获取其在内的所有好友
	public function getBygid($gid)
	{
		$sql="select * from $this->frd where gid=$gid";
		return $this->db->getAll($sql);
	}
	//获取用户的所有好友
	public function getAllfrd($uid)
	{
		$sql="select * from $this->frd where ownid=$uid";
		return $this->db->getAll($sql);
	}
	//获取用户的所有好友id
	public function getfrdid($uid)
	{
		$sql="select frdid from $this->frd where ownid=$uid";
		return $this->db->getAll($sql);
	}
	
	//通过uid查找其所有好友
	public function getbyuid($uid)
	{
		$sql="select * from $this->frd where ownid=$uid";
		return $this->db->getAll($sql);
	}

	//获取好友的信息
	public function getone($uid,$fid)
	{
		$sql="select * from $this->frd where ownid=$uid and frdid=$fid";
		return $this->db->getRow($sql);
	}

	//通过uid查询是否有这个好友
	//参数 本人id 要查询的好友id
	//-1 自己,0 没有此朋友,1 有此朋友
	public function isfrd($uid,$frdid)
	{
		if($uid==$frdid)
		{
			return -1;
		}
		$sql="select * from $this->frd where ownid=$uid and frdid=$frdid";
		$rs=$this->db->getAll($sql);
		if($rs)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
	
	//是否双方都是朋友
	public function bothfrd($uid,$frdid)
	{
		if($uid==$frdid)
		{
			return -1;//我自己
		}
		$sql="select * from $this->frd where ownid=$uid and frdid=$frdid";
		$myfrd=$this->db->getRow($sql);

		$sql="select * from $this->frd where ownid=$frdid and frdid=$uid";
		$frdmy=$this->db->getRow($sql);
		if(!empty($myfrd)&&!empty($frdmy))//互相好友
		{
			return 1;
		}
		else if(!empty($myfrd)&&empty($frdmy)) //我认识对方，对方不认识我
		{
			return 2;
		}
		else if(empty($myfrd)&&!empty($frdmy))//对方认识我，我不认识对方
		{
			return 3;
		}
		else//都不认识
		{
			return 0;
		}
	}


}
